@file:Suppress("UnstableApiUsage")

package me.rerere.unocssintellij.documentation

import com.intellij.lang.css.CSSLanguage
import com.intellij.lang.documentation.DocumentationMarkup
import com.intellij.model.Pointer
import com.intellij.openapi.application.readAction
import com.intellij.platform.backend.documentation.DocumentationResult
import com.intellij.platform.backend.documentation.DocumentationTarget
import com.intellij.platform.backend.presentation.TargetPresentation
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiFileFactory
import com.intellij.psi.css.CssDeclaration
import com.intellij.psi.css.CssElementFactory
import com.intellij.psi.css.impl.util.MdnDocumentationUtil
import me.rerere.unocssintellij.rpc.ResolveCSSResult
import me.rerere.unocssintellij.settings.UnocssSettingsState
import me.rerere.unocssintellij.util.appendHighlightedCss
import me.rerere.unocssintellij.util.appendUnocssColorPreview
import me.rerere.unocssintellij.util.childrenOfTypeDeeply
import me.rerere.unocssintellij.util.resolveRemToPx


class UnocssAttributeDocumentationTarget(
    private val targetElement: PsiElement?,
    private val result: ResolveCSSResult,
) : DocumentationTarget {

    override fun computePresentation(): TargetPresentation {
        return TargetPresentation
            .builder("Unocss Document")
            .presentation()
    }

    override fun createPointer() = Pointer.hardPointer(this)

    override fun computeDocumentation(): DocumentationResult? {
        val project = targetElement?.project ?: return null
        val cssFile: PsiFile = PsiFileFactory.getInstance(project)
            .createFileFromText(CSSLanguage.INSTANCE, resolveRemToPx(result.css, project))
        return DocumentationResult.asyncDocumentation {

            DocumentationResult.documentation(buildString {
                append(DocumentationMarkup.DEFINITION_START)
                append("<code>")
                appendHighlightedCss(project, cssFile.text)
                append("</code></pre>")

                appendUnocssColorPreview(project, result.css)

                append("""<div style="margin-top: 4px">Generated by Unocss</div>""")
                // definition end
                append("</div>")

                val settingsState = UnocssSettingsState.of(cssFile.project)
                // Generate Mdn Documentation
                if (settingsState.includeMdnDocs) {
                    val mdnDocumentations = readAction {
                        val cssElementFactory = CssElementFactory.getInstance(cssFile.project)
                        val tempElement = cssElementFactory
                            .createStylesheet(cssFile.text, CSSLanguage.INSTANCE)
                            .childrenOfTypeDeeply<CssDeclaration>()
                        tempElement.mapNotNull {
                            MdnDocumentationUtil.getMdnDocumentation(it, null)
                        }
                    }
                    append("""<div style="margin-bottom: 12px">""")
                    mdnDocumentations.forEach {
                        append(it.getDocumentation(false))
                    }
                    append("<div>")
                }
            })
        }
    }
}



